//
//  SettingViewController.m
//  mblove
//
//  Created by hsm on 11-12-22.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "SettingViewController.h"
#import "VcardTableViewController.h"
#import "TipTableViewController.h"
#import "PlanTableViewController.h"
#import "MatchTableViewController.h"
#import "ResumeTableViewController.h"
#import "UpdateTableViewController.h"
#import "AboutViewController.h"

#define tag_sound  1011
#define tag_sound2  1012
#define tag_vibrate  1013
#define tag_atuosit  1014
#define tag_autobuychip  1015
#define tag_endchip  1016
#define tag_update  10188
#define tag_V  108875
#define tag_finish  108879

#define RATE_URL_PREFIX    @"itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id="
#define RATE_URL_PREFIX_IOS7    @"itms-apps://itunes.apple.com/app/id"

@implementation SettingViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
     
        
    }
    return self;
}


-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [tableView reloadData];
}

-(void)dealloc{
    [NC removeObserver:self];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"设置";
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}
-(CGFloat)tableView:(UITableView *)aTableView heightForHeaderInSection:(NSInteger)section {
    return 20;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    int section = indexPath.section;
    if (section == 0) {
        return 80;
    }else{
        return 44;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    int num = 0;
    if (section == 0) {
        num = 1;
    }else if (section == 1) {
        num= 2;
    }else if (section == 2) {
        num= 4;
    }
    return  num;
}

- (UITableViewCell *)tableView:(UITableView *)tableview cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *CellIdentifier = @"SettingBaseCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    
    cell.backgroundColor = [UIColor clearColor];//ios7必须要这样处理，不然默认会是白色
    
    if (indexPath.section == 0) {
        UIButton *AdView = [[UIButton alloc] initWithFrame:RECT(5, 5, 70, 70)];
        AdView.userInteractionEnabled = NO;
        NSString *path = [RecInfo storePathWithFileName:ICUP_AVATAR];
        if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
            UIImage *image = [UIImage imageWithContentsOfFile:path];
            [AdView setBackgroundImage:image forState:UIControlStateNormal];
        }else{
            [AdView setBackgroundImage:IMG(@"头像--图标.png") forState:UIControlStateNormal];
        }
        [cell addSubview:AdView];
        
        for (int i=0; i<2; i++) {
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(FX(AdView)+FW(AdView)+8, FY(AdView)+12+i*25, 180, 20)];
            if (i == 0) {
                label.textColor = [UIColor blackColor];
//                label.text = @"海电";
            }else {
                label.textColor = [UIColor grayColor];
                
                if ([UD objectForKey:ICUP_CHANGE_GAO] && [UD objectForKey:ICUP_CHANGE_WEIGHT] && [UD objectForKey:ICUP_CHANGE_BIR]) {
                    label.text = [NSString stringWithFormat:@"%@cm,%@kg,%@", [UD objectForKey:ICUP_CHANGE_GAO], [UD objectForKey:ICUP_CHANGE_WEIGHT], [UD objectForKey:ICUP_CHANGE_BIR]];
                }
                
                else if ([UD objectForKey:ICUP_CHANGE_GAO] && [UD objectForKey:ICUP_CHANGE_WEIGHT]) {
                    label.text = [NSString stringWithFormat:@"%@cm,%@kg", [UD objectForKey:ICUP_CHANGE_GAO], [UD objectForKey:ICUP_CHANGE_WEIGHT]];
                }else if ([UD objectForKey:ICUP_CHANGE_GAO] && [UD objectForKey:ICUP_CHANGE_BIR]) {
                    label.text = [NSString stringWithFormat:@"%@cm,%@", [UD objectForKey:ICUP_CHANGE_GAO],  [UD objectForKey:ICUP_CHANGE_BIR]];
                }else if ([UD objectForKey:ICUP_CHANGE_WEIGHT] && [UD objectForKey:ICUP_CHANGE_BIR]) {
                    label.text = [NSString stringWithFormat:@"%@kg,%@", [UD objectForKey:ICUP_CHANGE_WEIGHT], [UD objectForKey:ICUP_CHANGE_BIR]];
                }

                else if ([UD objectForKey:ICUP_CHANGE_GAO]) {
                    label.text = [NSString stringWithFormat:@"%@cm", [UD objectForKey:ICUP_CHANGE_GAO]];
                }else if ([UD objectForKey:ICUP_CHANGE_WEIGHT]) {
                    label.text = [NSString stringWithFormat:@"%@kg", [UD objectForKey:ICUP_CHANGE_WEIGHT]];
                }else if ([UD objectForKey:ICUP_CHANGE_BIR]) {
                    label.text = [NSString stringWithFormat:@"%@", [UD objectForKey:ICUP_CHANGE_BIR]];
                }
            }
            label.font = [UIFont systemFontOfSize:16];
            label.backgroundColor = [UIColor clearColor];
            [cell addSubview:label];
        }
    }else if(indexPath.section == 1){
        if (indexPath.row == 0) {
            cell.textLabel.text = @"提醒";
        }else{
            cell.textLabel.text = @"饮水计划";
        }
    }else if(indexPath.section == 2){
        if (indexPath.row == 0) {
            cell.textLabel.text = @"配对信息";
        }else if (indexPath.row == 1){
            cell.textLabel.text = @"恢复出厂设置";
        }else if (indexPath.row == 2){
            cell.textLabel.text = @"软件升级";
        }else if (indexPath.row == 3){
            cell.textLabel.text = @"关于我们";
        }
    }
    return cell;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)atableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [atableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.section ==0 ) {
        
        VcardTableViewController *view = [[VcardTableViewController alloc] initWithStyle:UITableViewStylePlain];
        [self.navigationController pushViewController:view animated:YES];
        
    }else if (indexPath.section ==1) {
        if (indexPath.row==0) {
            TipTableViewController *view = [[TipTableViewController alloc] initWithStyle:UITableViewStylePlain];
            [self.navigationController pushViewController:view animated:YES];
        }else if (indexPath.row ==1) {
            PlanTableViewController *view = [[PlanTableViewController alloc] initWithStyle:UITableViewStylePlain];
            [self.navigationController pushViewController:view animated:YES];
        }
        
    }else  if (indexPath.section ==2) {
        if (indexPath.row ==0) {
            MatchTableViewController *view = [[MatchTableViewController alloc] initWithStyle:UITableViewStylePlain];
            [self.navigationController pushViewController:view animated:YES];
        }else  if (indexPath.row ==1) {
            ResumeTableViewController *view = [[ResumeTableViewController alloc] initWithStyle:UITableViewStylePlain];
            [self.navigationController pushViewController:view animated:YES];
        }else  if (indexPath.row ==2) {
            UpdateTableViewController *view = [[UpdateTableViewController alloc] initWithStyle:UITableViewStylePlain];
            [self.navigationController pushViewController:view animated:YES];
        }else  if (indexPath.row ==3) {
            
            AboutViewController *view = [[AboutViewController alloc] init];
            [self.navigationController pushViewController:view animated:YES];
        }
    }
}

@end
